//
// Created by tang on 23-3-25.
//

#include "gl_render_service.hpp"
#include "imgui_ext/texture.h"
#include "utils/io_tools.hpp"
#include "ui/ui_item.hpp"

image_handle gl_render_service::create_image_from_content (const u8 *dat, u32 size)
{
    auto texture = new Texture {};
    texture->LoadFromMem (dat, size);
    return texture;
}
image_handle gl_render_service::create_image_from_file (const std::string &path)
{
    auto texture = new Texture {};
    texture->LoadFromFile (path.c_str());
    return texture;
}

void gl_render_service::free_image (image_handle img) {
    auto *texture = (Texture *)img;
    delete texture;
}

ui_view *gl_render_service::create_view_from_data (const u8 *dat, u32 size)
{
    return ui_item::create_ui (dat, size);
}
ui_view *gl_render_service::create_view_from_file (const std::string &path)
{
    ui_view *ret = nullptr;
    auto text = ReadTextFile(path);
    if (!text.empty())
        ret = create_view_from_data ((const u8*)text.data(), text.length());
    return ret;
}
